package ch13;

import java.util.List;

public class PersonUI {
	private PersonDao dao ;	
	
	public PersonUI(){
		dao = new PersonDao();		
	}		
	
	private void start(){
		deleteAllPerson();
		insertPerson(new Person(1,"john"));
		insertPerson(new Person(2,"terry"));
		selectAllPerson();
		updatePerson(new Person(1,"kara"));
		selectPerson(1);
		deletePerson(1);
		selectAllPerson();
	}
	
	private void selectAllPerson(){
		List<Person> list = dao.selectAllPerson();
		if(list != null && list.size() != 0)
			for(Person person:list)
				System.out.printf("[%d, %s]\n",person.getId(),person.getName());
		else System.out.println("no Person.");
	}
	private void selectPerson(int id) {
		Person person = dao.selectPerson(id);
		if(person != null)
			System.out.printf("[%d, %s]\n", person.getId(),person.getName());
		else System.out.println("no Person.");
	}
	
	private void insertPerson(Person person) {
		dao.insertPerson(person);
		System.out.println("SUCCESS: insert.");
	}
	
    private void updatePerson(Person person) {
    	dao.updatePerson(person);
    	System.out.println("SUCCESS: update.");
    }
    
    private void deletePerson(int id) {
    	dao.deletePerson(id);
    	System.out.println("SUCCESS: delete.");
    }
    
    private void deleteAllPerson() {
    	dao.deleteAllPerson();
    	System.out.println("SUCCESS: delete for init.");
    }
	
	public static void main(String[] args){
		new PersonUI().start();
	}
}
/*
SUCCESS: delete for init.
SUCCESS: insert.
SUCCESS: insert.
[1, john]
[2, terry]
SUCCESS: update.
[1, kara]
SUCCESS: delete.
[2, terry]
*/
